package com.yanghui.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanghui.pojo.Log;
import com.yanghui.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("all")
@RequestMapping("/admin")
@Controller
public class LogControllerAdmin {
    @Autowired
    private LogService logService;

    /**
     * 按照指定日期查询操作日志
     */
    @RequestMapping(value = "/selectUserLogByDate",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> selectBlogListByDate(
            Log log,
            @RequestParam(value="startTime") String startTime,
            @RequestParam(value="endTime") String endTime) {
        /** 封装查询条件 */
        Map<String, Object> map = new HashMap<>();
        if (log.getUserType() != null && "".equals(log.getUserType()) != true) {
            map.put("userType", log.getUserType());
        }
        if (startTime != null && "".equals(startTime) != true) {
            map.put("startTime", startTime);
        }
        if (endTime != null && "".equals(endTime) != true) {
            map.put("endTime", endTime);
        }

        List<?> list = logService.selectUserLogByDate(map);
        Map<String, Object> resultMap = new HashMap<>();

        if(list.size()>0){
            resultMap.put("status", 200);
        }else{
            //500表示：返回值为Null
            resultMap.put("status", 500);
        }
        resultMap.put("list", list);

        return resultMap;
    }


    /**
     * 按照查询条件查询日志
     */
    @RequestMapping("/selectLogListByDate")
    @ResponseBody
    public Map<String,Object> selectLogListByDate(
            Log log,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "10")Integer pageSize,
            @RequestParam("startTime") String startTime,
            @RequestParam("endTime") String endTime) {
        /** 封装查询条件 */
        Map<String,Object> map = new HashMap<>();
        if (log.getIp() != null && "".equals(log.getIp()) != true) {
            map.put("ip", log.getIp());
        }
        if (log.getUserType() != null && "".equals(log.getUserType()) != true) {
            map.put("userType", log.getUserType());
        }
        if (log.getDescription() != null && "".equals(log.getDescription()) != true) {
            map.put("description", log.getDescription());
        }
        if (startTime != null && "".equals(startTime) != true) {
            map.put("startTime", startTime);
        }
        if (endTime != null && "".equals(endTime) != true) {
            map.put("endTime", endTime);
        }

        PageHelper.startPage(page, pageSize);
        List<Log> logList = logService.selectLogListByDate(map);
        PageInfo pageInfo = new PageInfo(logList);
        Map<String,Object> resultMap = new HashMap<>();

        if (logList.size() > 0) {
            map.put("status", 200);
        } else {
            map.put("status", 500);
        }
        map.put("logList", logList);
        map.put("pageInfo", pageInfo);

        return map;
    }
}
